package org.example.design.designmode.proxy.cglibproxy;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.example.design.designmode.proxy.cglibproxy.PeopleService;

import java.lang.reflect.Method;

public class CGIBProxy implements MethodInterceptor {

    private PeopleService trget;

    public Object getInstance(Class clazz) {
        System.out.println("启动代理！");
        Enhancer en = new Enhancer();
        en.setSuperclass(clazz);
        en.setCallback(this);
       return en.create();


    }





    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("调用代理底层！");
        before();
        Object ob = methodProxy.invokeSuper(o,objects);
        System.out.println("me.invoke(this.trget, objects)"+ob);
        after();
        return ob;
    }

    private void before() {
        System.out.println("开始物色！");
    }

    private void after() {
        System.out.println("双方确认交往！");
    }

}
